package weaver.generalledger.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;
import weaver.workflow.workflow.WorkflowComInfo;

/**
 *  归档时触发
 */
public class AddGeneralledgerDatasAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        //根据总账单.分录明细.入账公司字段与总账单.凭证明细.所属法人主体对应的凭证明细的数据，
        // 插入对应入账公司的分录明细字段和凭证明细字段（凭证日期、凭证号、凭证类型），归档时触发
        writeLog(" AddGeneralledgerDatasAction ："+requestInfo);
        RequestManager requestManager = requestInfo.getRequestManager();
        String requestid = requestInfo.getRequestid();
        String workflowid = requestInfo.getWorkflowid();
        String nodetype = requestManager.getNodetype();
        String formId = (new WorkflowComInfo()).getFormId(workflowid);
        String tableName = getNameById(formId);
        String lastoperator = requestInfo.getLastoperator();
        RecordSet rs = new RecordSet();

        RecordSet recordSet = new RecordSet();
        writeLog("workflowid："+workflowid+"，tableName："+tableName+"，requestid："+requestid+"，formId："+formId);

        String sql="select * from "+tableName+" where requestId=?";
        writeLog("sql :"+sql);
        recordSet.executeQuery(sql, new Object[]{requestid});
        if (recordSet.next()) {
            String lcbm = recordSet.getString("lcbm");
            String id=recordSet.getString("id");

            String tableNameDt1=tableName+"_dt1";
            String dt1Sql="select * from "+tableNameDt1+" where mainid=?";
            writeLog("dt1Sql :"+dt1Sql);
            RecordSet dt1RecordSet = new RecordSet();
            dt1RecordSet.executeQuery(dt1Sql, new Object[]{id});
            while(dt1RecordSet.next()){
                RecordSet ztRecordSet = new RecordSet();

                String hszt=dt1RecordSet.getString("hszt");
                String ztSql="select ztbm from uf_jccs_zt where id=?";
                ztRecordSet.executeQuery(ztSql, new Object[]{hszt});
                String hsztbm="";
                if(ztRecordSet.next()){
                    hsztbm= ztRecordSet.getString("ztbm");
                }

                String tableNameDt2=tableName+"_dt2";
                String dt2Sql="select voucherDate,voucherUser,voucherDataType,voucherNumber from "+tableNameDt2+" where mainid=? and companyCode=?";

                RecordSet dt2RecordSet = new RecordSet();
                dt2RecordSet.executeQuery(dt2Sql, new Object[]{id,hsztbm});
                String voucherDate="";
                String voucherUser="";
                String voucherDataType="";
                String voucherNumber="";
               if(dt2RecordSet.next()){
                    voucherDate=dt2RecordSet.getString("voucherDate");
                    voucherUser=dt2RecordSet.getString("voucherUser");
                    voucherDataType=dt2RecordSet.getString("voucherDataType");
                    voucherNumber=dt2RecordSet.getString("voucherNumber");
               }

                writeLog(dt2Sql);
                writeLog("voucherDate:"+voucherDate+"voucherUser:"+voucherUser+"voucherDataType:"+voucherDataType+"voucherNumber:"+voucherNumber+"hszt:"+hszt+"main:"+id);


                RecordSet zzdtzRecordSet = new RecordSet();

                String inSql="insert into uf_glrepirt (formmodeid,voucherDate,voucherUser,voucherDataType,bm,bz,hl,jfwb,dfwb,jfje,dfje,xzy,gr," +
                        "jd,hjkm,czfs,yglx,yhzh,yhlb,gdlx,nbwlgsd,tzdx,kjxm,pz,hsywfl,pzh,ks,hszt,hdlx,htbh,zzddh,xm)"+
                        "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";

                String formmodeid=getFormModeId("uf_glrepirt",rs);

              String  bm=         dt1RecordSet.getString("bm");
                String  bz=          dt1RecordSet.getString("bz");
                Double  hl=        conversionDouble(dt1RecordSet.getString("hl"));
                Double  jfwb=       conversionDouble(dt1RecordSet.getString("jfwb"));
                Double  dfwb=       conversionDouble(dt1RecordSet.getString("dfwb"));
                Double  jfje=      conversionDouble(dt1RecordSet.getString("jfje"));
                Double  dfje=      conversionDouble(dt1RecordSet.getString("dfje"));
                String  xzy=   dt1RecordSet.getString("xzy");
                String  gr=      dt1RecordSet.getString("gr");
                String  jdfx=      dt1RecordSet.getString("jdfx");
                String  hjkm=      dt1RecordSet.getString("hjkm");
                String  czfs=       dt1RecordSet.getString("czfs");
                String  yglx=       dt1RecordSet.getString("yglx");
                String  yhzh=       dt1RecordSet.getString("yhzh");
                String  yhlb=       dt1RecordSet.getString("yhlb");
                String  gdlx=       dt1RecordSet.getString("gdlx");
                String  nbwlgsd=       dt1RecordSet.getString("nbwlgsd");
                String  tzdx=       dt1RecordSet.getString("tzdx");
                String  kjxm=       dt1RecordSet.getString("kjxm");
                String  pz=        dt1RecordSet.getString("pz");
                String  hsywfl=       dt1RecordSet.getString("hsywfl");
                String  ks=       dt1RecordSet.getString("ks");
                String  hdlx=       dt1RecordSet.getString("hdlx");
                String  htbh=       dt1RecordSet.getString("htbh");
                String  xm=       dt1RecordSet.getString("xm");



                zzdtzRecordSet.executeUpdate(inSql,Integer.parseInt(formmodeid),voucherDate,voucherUser,voucherDataType,dt1RecordSet.getString("bm"),
                        dt1RecordSet.getString("bz"),
                        conversionDouble(dt1RecordSet.getString("hl")),conversionDouble(dt1RecordSet.getString("jfwb")),
                        conversionDouble(dt1RecordSet.getString("dfwb")),conversionDouble(dt1RecordSet.getString("jfje")),
                        conversionDouble(dt1RecordSet.getString("dfje")),dt1RecordSet.getString("xzy"),conversionNull(gr),
                        conversionNull(jdfx),conversionNull(hjkm),conversionNull(czfs),
                        conversionNull(yglx),conversionNull(yhzh)
                        ,conversionNull(yhlb),conversionNull(gdlx),conversionNull(nbwlgsd),
                        conversionNull(tzdx),conversionNull(kjxm),conversionNull(pz)
                        ,conversionNull(hsywfl),voucherNumber,conversionNull(ks),hszt,conversionNull(hdlx),
                        conversionNull(htbh),lcbm, conversionNull(xm));
            }

            //归档时将总账单.凭证明细.凭证号反写至总账工单台账.总账单记账凭证号，
            // 此时一张总账单只会生成一条凭证，与总账工单号一一对应，归档时触发
            String zzgdh=recordSet.getString("zzgdh");
            writeLog("get zzgdh:"+zzgdh);
            String zzgdtzSql="select * from uf_zongzhanggongda where lcbm=?";
            writeLog("zzgdtzSql :"+zzgdtzSql);
            RecordSet zzgdtzRecordSet = new RecordSet();
            zzgdtzRecordSet.executeQuery(zzgdtzSql, new Object[]{zzgdh});
            writeLog(zzgdh);
            if(zzgdtzRecordSet.next()) {
                String hszt=recordSet.getString("hszt");
                String zzgdtzrzgs = zzgdtzRecordSet.getString("rzgs");

                writeLog("hszt:"+hszt+"zzgdtzrzgs:"+zzgdtzrzgs);
                if(zzgdtzrzgs.equals(hszt)){
                    String tableNameDt21=tableName+"_dt2";
                    String dt21Sql="select voucherDate,voucherUser,voucherDataType,voucherNumber from "+tableNameDt21+" where mainid=?";
                    writeLog(dt21Sql+id);
                    writeLog("dt21Sql :"+dt21Sql);
                    RecordSet dt21RecordSet = new RecordSet();
                    dt21RecordSet.executeQuery(dt21Sql, new Object[]{id});
                    if(dt21RecordSet.next()){
                        RecordSet zzgdtzSet=new RecordSet();
                        String pzdh=dt21RecordSet.getString("voucherNumber");

                        String zzgdtzUpSql="update uf_zongzhanggongda set zzdjzpzh=? where lcbm=?";
                        writeLog(zzgdtzUpSql+zzgdh);
                        zzgdtzSet.executeUpdate(zzgdtzUpSql,pzdh,zzgdh);
                    }

                }
            }
        }

        return null;
    }

    public Double conversionDouble(String num){
        if(num!=null&& !num.equals("")){
            return Double.parseDouble(num);
        }else{
            return 0.0;
        }
    }

    public String conversionNull(String str){
        if(str==null || str.equals("")){
            return "0";
        }else{
            return str;
        }
    }

    /**
     * 根据tableName 获取FormModeId
     * @param tableName
     * @param rs
     * @return
     */
    private String getFormModeId(String tableName, RecordSet rs) {
        String sql = "select m.id from modeinfo m inner join workflow_bill wb on m.formid=wb.id where wb.tablename= ?";
        boolean b = rs.executeQuery(sql, tableName);
        if(b && rs.next()) {
            return Util.null2String(rs.getString(1));
        }else {
            return "";
        }
    }

    public static String getNameById(String var0) {
        RecordSet var1 = new RecordSet();
        String var2 = "";
        String var3 = "select tablename from workflow_bill where id=?";
        var1.executeQuery(var3, new Object[]{var0});
        if (var1.next()) {
            var2 = var1.getString("tablename");
        }

        return var2;
    }
}
